const router = require('koa-router')()
const { login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')

router.prefix('/api/user')

// 登录
router.post('/login', async (ctx, next) => {
    const { username, password } = ctx.request.body;
    const result = await login(username, password);

    if (result.username) {
        ctx.session.username = result.username;
        ctx.session.realname = result.realname;
        ctx.body = new SuccessModel();
    } else {
        ctx.body = new ErrorModel('登录失败');
    }
})

router.get('/get-session', async (ctx, next) => {
    if (ctx.session.viewNum == null) {
        ctx.session.viewNum = 0;
    } else {
        ctx.session.viewNum++;
    }

    ctx.body = {
        errno: 0,
        viewNum: ctx.session.viewNum
    }
})

module.exports = router